package dev.huidocs.docs.rest;

import com.gitee.coadmin.annotation.UnifiedAPI;
import com.gitee.coadmin.exception.CoException;
import com.gitee.coadmin.utils.ExcelUtils;
import com.gitee.coadmin.base.PageInfo;
import com.gitee.coadmin.modules.logging.annotation.Log;
import com.gitee.coadmin.modules.logging.annotation.type.LogActionType;
import dev.huidocs.docs.service.DocResAccessedRecentlyService;
import dev.huidocs.docs.service.dto.DocResAccessedRecentlyDTO;
import dev.huidocs.docs.service.dto.DocResAccessedRecentlyQueryParam;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Set;

/*  添加菜单的 SQL
*/

/**
 * @author jinjin
 * @since 2024-10-11
 **/
@UnifiedAPI
@RestController
@RequiredArgsConstructor
@Api(tags = "Accessed recently")
@RequestMapping("/api/docs/doc-res-accessed-recently")
public class DocResAccessedRecentlyController {

    private final DocResAccessedRecentlyService docResAccessedRecentlyService;

    @GetMapping
    @Log("查询Accessed recently")
    @ApiOperation("查询Accessed recently")
    @PreAuthorize("@el.check('docResAccessedRecently:list')")
    public PageInfo<DocResAccessedRecentlyDTO> query(DocResAccessedRecentlyQueryParam query, Pageable pageable){
        return docResAccessedRecentlyService.pageByParam(query,pageable);
    }

    @PostMapping
    @Log(value = "新增Accessed recently", type = LogActionType.ADD)
    @ApiOperation("新增Accessed recently")
    @PreAuthorize("@el.check('docResAccessedRecently:add')")
    public void create(@Validated @RequestBody DocResAccessedRecentlyDTO res){
        docResAccessedRecentlyService.insert(res);
    }

    @PutMapping
    @Log(value = "修改Accessed recently", type = LogActionType.UPDATE)
    @ApiOperation("修改Accessed recently")
    @PreAuthorize("@el.check('docResAccessedRecently:edit')")
    public void update(@Validated @RequestBody DocResAccessedRecentlyDTO res){
        docResAccessedRecentlyService.updateById(res);
    }

    @DeleteMapping
    @Log(value = "删除Accessed recently", type = LogActionType.DELETE)
    @ApiOperation("删除Accessed recently")
    @PreAuthorize("@el.check('docResAccessedRecently:del')")
    public void delete(@RequestBody Set<Long> ids) {
        docResAccessedRecentlyService.removeByIds(ids);
    }

    @Log("导出Accessed recently")
    @ApiOperation("导出Accessed recently")
    @UnifiedAPI(enable = false)
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('docResAccessedRecently:down')")
    public void download(DocResAccessedRecentlyQueryParam criteria, HttpServletResponse response) {
        try {
            List<DocResAccessedRecentlyDTO> dtos = docResAccessedRecentlyService.listByParam(criteria);
            ExcelUtils.exportExcel(dtos, null, "导出Accessed recently", DocResAccessedRecentlyDTO.class, "", response);
        } catch (IOException e) {
            throw new CoException("导出失败");
        }
    }
}
